package com.metadata_system.common.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import com.metadata_system.common.dto.ApiResult;
import com.metadata_system.common.utils.DateTimeUtil;
import com.metadata_system.common.utils.RedisUtil;
import com.metadata_system.common.utils.SliderCaptchaUtils;
import com.metadata_system.common.utils.UploadFileUtil;

import java.io.File;
import java.util.*;

@Service
@Slf4j
public class SliderCaptchaService {
    @Autowired
    RedisUtil redisUtil;
    @Autowired
    UploadFileUtil uploadFileUtil;
    // 允许误差值
    public static Integer AllowDiff = 5;
    public static String RedisPreFix = "SLIDER_CAPTCHA__";
    @Value("${metadata-system.upload-path:/www/wwwroot/upload-file.metadata-system.cc/}")
    private String uianUploadPath;

    // 上传文件-访问域名,默认(http://tsca-upload-file.cc/)
    @Value("${metadata-system.upload-url:http://upload-file.metadata-system.cc}")
    private String uianUploadDoname;
    @Value("${metadata-system.slider-captcha-images-dir}")
    public String sliderCaptchaImagesDir = "F:/metadata-system/metadata-system-api/file/slider_captcha_images";

    @Autowired
    private Environment env;

    public static String GetRedisKey(String deviceId, String eventType) {
        return RedisPreFix + eventType + "__" + deviceId;
    }

    public ApiResult generate(String deviceId, String eventType) {

        String uploadDir = "/slider-captcha/" + DateTimeUtil.formatDateTimetoString(new Date(), DateTimeUtil.FMT_yyyyMMdd) + "/";
        File file = new File(uianUploadPath + uploadDir);


//        System.out.println("file path: "+uianUploadPath + uploadDir);
        if (!file.isDirectory()) {
            file.mkdirs();
        }
        String fileName= DateTimeUtil.formatDateTimetoString(new Date(), DateTimeUtil.FMT_yyyyMMdd)+UUID.randomUUID() + ".png";
        String imagePath = uploadDir + fileName;
        Map<String, Object> generateMap = SliderCaptchaUtils.genarateImage(uianUploadPath + imagePath, sliderCaptchaImagesDir);

        String sliderImgUrl = uianUploadDoname + imagePath;

        HashMap<String, Object> backMap = new HashMap<>();
        backMap.put("sliderImg", sliderImgUrl);
        backMap.put("sliderKey", deviceId);
        backMap.put("sliderY", (Integer) generateMap.get("Y"));
        redisUtil.setValue(GetRedisKey(deviceId, eventType), generateMap.get("X").toString());


        // 生产环境上传至oss
//        if (Objects.equals(env.getProperty("spring.profiles.active"), "prod")){
//            String ossUrl=uploadFileUtil.uploadFileToOss(uianUploadPath + imagePath,"slider-captcha/"+fileName);
//            backMap.put("sliderImg", ossUrl);
//        }

//        String ossUrl=uploadFileUtil.uploadFileToOss(uianUploadPath + imagePath,"slider-captcha/"+fileName);
        backMap.put("sliderImg", "slider-captcha/"+fileName);

        return ApiResult.success(backMap);
    }


    public ApiResult check(String deviceId, String eventType, Integer X) {
        String redisKey = GetRedisKey(deviceId, eventType);
//        log.info("redisKey: "+redisKey);
        Integer redisX = Integer.valueOf(redisUtil.getValue(redisKey, String.class));
        if ((X - redisX) <= AllowDiff && X - redisX >= -AllowDiff) {
            return ApiResult.success();
        }
        return ApiResult.error("滑动验证码 错误");
    }

    public void clear(String deviceId, String eventType) {
        this.redisUtil.del(GetRedisKey(deviceId, eventType));
    }
}
